package com.aaa.sbms.service;

import com.aaa.sbms.dao.TrainDao;
import com.sun.imageio.plugins.common.I18N;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @FileName:TrainServiceImpl
 * @Description:
 * @Author :99728
 * @Date: 2020/6/17 15:56
 * @Version:1.0.0
 */
@Service
public class TrainServiceImpl implements TrainService {

    @Autowired(required = false)
    private TrainDao trainDao;
//
    @Override
    public Map getPage(Map map) {
        map.put("begin",(Integer.valueOf(map.get("pageNo")+"")-1)* Integer.valueOf(map.get("pageSize")+""));
        Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
        map.put("pageSize",pageSize);
        //调用dao获取数据
        List<Map> page = trainDao.getPage(map);
        int total = trainDao.getPageCount(map);
        //合并
        Map resultMap = new HashMap();
        resultMap.put("page",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map getCourseName(Map map) {
        Map resultMap=new HashMap();
        List<Map> courseName = trainDao.getCourseName(map);
        resultMap.put("list",courseName);
        return resultMap;
    }

    @Override
    public List<Map> getTrainerName(Map map) {
        return trainDao.getTrainerName(map);
    }

    @Override
    public Map getUserName(Map map) {
        Map resultMap = new HashMap();
        List<Map> userName = trainDao.getUserName(map);
        resultMap.put("list",userName);
        return resultMap;
    }


    /**
     * 添加
     * @param map
     * @return
     */
    @Override
    public int add(Map map) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //日期选择器获取的是String类型的
        String starttime = map.get("starttime").toString();
        String endtime = map.get("endtime").toString();
        try {
            Date starttime1 = sdf.parse(starttime);
            Date endtime1 = sdf.parse(endtime);
            map.put("starttime", starttime1);
            map.put("endtime", endtime1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
            return trainDao.add(map);
    }

    /**
     * 删除
     * @param trainid
     * @return
     */
    @Override
    public int delete(int trainid) {
        return trainDao.delete(trainid);
    }


}
